package com.ly.web.controller.erp;

import java.util.List;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ly.heart.domain.MaterialDetailEntity;
import com.ly.heart.domain.OrderEntity;
import com.ly.heart.domain.SaleMarkedPriceEntity;
import com.ly.heart.domain.dto.CheckSaleMarkedPriceDto;
import com.ly.heart.domain.dto.SaleOrderAddDto;
import com.ly.heart.domain.dto.SaleOrderQueryDto;
import com.ly.heart.domain.vo.ContractOrderMaterialDetailVo;
import com.ly.heart.domain.vo.SaleOrderDetailVo;
import com.ly.heart.service.king.SaleOrderPushService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ly.common.annotation.Log;
import com.ly.common.core.controller.BaseController;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.enums.BusinessType;
import com.ly.heart.domain.SaleOrderEntity;
import com.ly.heart.service.ISaleOrderService;
import com.ly.common.utils.poi.ExcelUtil;
import com.ly.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 销售订单Controller
 * @author wutao
 * @date 2024-06-26
 */
@Api (value = "SaleOrder", tags = "销售订单")
@RestController
@RequestMapping ("/heart/SaleOrder")
public class SaleOrderController extends BaseController {

    @Resource
    private ISaleOrderService saleOrderService;
    @Resource
    private SaleOrderPushService saleOrderPushService;

    /**
     * 选单调用的销售订单列表
     */
    @PreAuthorize ("@ss.hasPermi('heart:SaleOrder:list')")
    @GetMapping ("/list")
    @ApiOperation ("选单调用的销售订单列表")
    public AjaxResult list (SaleOrderQueryDto saleOrderEntity) {
        IPage<SaleOrderDetailVo> saleOrderDetailVoIPage = saleOrderService.selectSaleOrderDetailList ( saleOrderEntity );
        return AjaxResult.success ( saleOrderDetailVoIPage );
    }

    /**
     * 查询销售订单列表
     * @param saleOrderQueryDto
     * @return
     */

    @GetMapping ("/listSaleOrderDetail")
    @ApiOperation ("查询销售订单列表")
    public AjaxResult listSaleOrderDetail (SaleOrderQueryDto saleOrderQueryDto) {
        IPage<SaleOrderDetailVo> saleOrderDetailVoIPage = saleOrderService.selectSaleOrderDetailListNew ( saleOrderQueryDto );
        return AjaxResult.success ( saleOrderDetailVoIPage );
    }


    /**
     * 导出销售订单列表
     */
    @PreAuthorize ("@ss.hasPermi('heart:SaleOrder:export')")
    @Log (title = "销售订单", businessType = BusinessType.EXPORT)
    @GetMapping ("/export")
    @ApiOperation ("导出销售订单列表")
    public AjaxResult export (SaleOrderEntity saleOrder) {
        List<SaleOrderEntity> list = saleOrderService.selectSaleOrderList ( saleOrder );
        ExcelUtil<SaleOrderEntity> util = new ExcelUtil<SaleOrderEntity> ( SaleOrderEntity.class );
        return util.exportExcel ( list, "order" );
    }

    /**
     * 获取销售订单详细信息
     */
    @PreAuthorize ("@ss.hasPermi('heart:SaleOrder:query')")
    @GetMapping (value = "/{id}")
    @ApiOperation ("获取销售订单详细信息")
    public AjaxResult getInfo (@PathVariable ("id") String id) {
        return AjaxResult.success ( saleOrderService.selectSaleOrderById ( id ) );
    }

    @PreAuthorize ("@ss.hasPermi('heart:SaleOrder:add')")
    @Log (title = "暂存销售订单", businessType = BusinessType.INSERT)
    @PostMapping ("draft")
    @ApiOperation ("暂存销售订单")
    public AjaxResult draft (@RequestBody SaleOrderAddDto saleOrderAddDto) {
        AjaxResult ajaxResult = saleOrderService.draft ( saleOrderAddDto );
        return AjaxResult.success ( ajaxResult );
    }

    @PreAuthorize ("@ss.hasPermi('heart:SaleOrder:add')")
    @Log (title = "提交销售订单", businessType = BusinessType.INSERT)
    @PostMapping ("submit")
    @ApiOperation ("提交销售订单")
    public AjaxResult submit (@RequestBody SaleOrderAddDto saleOrderAddDto) {
        AjaxResult ajaxResult = saleOrderService.submit ( saleOrderAddDto );
        return AjaxResult.success ( ajaxResult );
    }

    /**
     * 修改销售订单
     */
    @PreAuthorize ("@ss.hasPermi('heart:SaleOrder:edit')")
    @Log (title = "销售订单", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation ("修改销售订单")
    public AjaxResult edit (@RequestBody SaleOrderAddDto saleOrderAddDto) {
        return saleOrderService.updateSaleOrder ( saleOrderAddDto );
    }

    /**
     * 删除销售订单
     */
    @PreAuthorize ("@ss.hasPermi('heart:SaleOrder:remove')")
    @Log (title = "销售订单", businessType = BusinessType.DELETE)
    @DeleteMapping ("/{ids}")
    @ApiOperation ("删除销售订单")
    public AjaxResult remove (@PathVariable String[] ids) {
        return toAjax ( saleOrderService.deleteSaleOrderByIds ( ids ) );
    }


    /**
     * 根据采购合同对象List查询物料明细
     */
    @Log (title = "新增销售合同，通过选销售订单时，查询销售订单物料明细列表")
    @GetMapping ("queryMaterial")
    @ApiOperation ("根据销售订单选单查询物料明细")
    public TableDataInfo queryMaterial () {
        startPage ();
        List<ContractOrderMaterialDetailVo> materialDetails = saleOrderService.queryMaterial ();
        return getDataTable ( materialDetails );
    }


    @PostMapping ("/checkMarkedPrice")
    @ApiOperation ("检查销售订单价格是否正确")
    public AjaxResult checkMarkedPrice (@RequestBody CheckSaleMarkedPriceDto checkSaleMarkedPriceDto) {
        return AjaxResult.success ( saleOrderService.checkSaleOrderDetailByPriceInfo ( checkSaleMarkedPriceDto ) );
    }

    @GetMapping ("/selectSaleChangeOrderList")
    @ApiOperation ("销售变更单查询")
    public AjaxResult selectSaleChangeOrderList (SaleOrderQueryDto saleOrderQueryDto) {
        return AjaxResult.success ( saleOrderService.selectSaleOrderChangeListByMaterial ( saleOrderQueryDto ) );
    }

    @GetMapping ("/send")
    @ApiOperation ("推送销售订单到金蝶")
    public AjaxResult send (String id) {
        saleOrderPushService.push ( id );
        return AjaxResult.success ( "推送成功" );
    }


}
